package servlet4;

import com.google.gson.Gson;
import dao.DB;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;

//修改个人信息
@WebServlet("/upUserdata")
public class UpUserdata extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        Cookie[] cookies = req.getCookies();
        String newToken = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("TOKEN")) {
                newToken = cookie.getValue();
            }
        }

//        String birthTimeString =  DB.executeQuery("SELECT ageDate FROM `user` WHERE token = ?" ,newToken).get(0).get("ageDate").toString();
        String acc =  DB.executeQuery("SELECT account FROM `user` WHERE token = ?" ,newToken).get(0).get("account").toString();
        String name =req.getParameter("upName1");
        String upSex =req.getParameter("upSex1");
        String upAge =req.getParameter("upAge1");
        String upAddress =req.getParameter("upAddress1");
        String upOccuption =req.getParameter("upOccuption1");
        String upPhone =req.getParameter("upPhone1");



        String strs[] = upAge.trim().split("-");
        int selectYear = Integer.parseInt(strs[0]);
        int selectMonth = Integer.parseInt(strs[1]);
        int selectDay = Integer.parseInt(strs[2]);
        // 得到当前时间的年、月、日
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayNow = cal.get(Calendar.DATE);

        // 用当前年月日减去生日年月日
        int yearMinus = yearNow - selectYear;
        int monthMinus = monthNow - selectMonth;
        int dayMinus = dayNow - selectDay;

        int age = yearMinus;// 先大致赋值
        if (yearMinus < 0) {// 选了未来的年份
            age = 0;
        } else if (yearMinus == 0) {// 同年的，要么为1，要么为0
            if (monthMinus < 0) {// 选了未来的月份
                age = 0;
            } else if (monthMinus == 0) {// 同月份的
                if (dayMinus < 0) {// 选了未来的日期
                    age = 0;
                } else if (dayMinus >= 0) {
                    age = 1;
                }
            } else if (monthMinus > 0) {
                age = 1;
            }
        } else if (yearMinus > 0) {
            if (monthMinus < 0) {// 当前月>生日月
            } else if (monthMinus == 0) {// 同月份的，再根据日期计算年龄
                if (dayMinus < 0) {
                } else if (dayMinus >= 0) {
                    age = age + 1;
                }
            } else if (monthMinus > 0) {
                age = age + 1;
            }
        }





        int tmp = DB.executeUpdate("UPDATE `user` SET" +
                "  `nickname` = ?, `sex` = ?,`age` = ?, `ageDate` = ?, `address` = ?,  `occuption` = ?, `phone` = ? WHERE account = ?;",
                name,upSex,age,upAge,upAddress,upOccuption,upPhone,acc

        );



        resp.sendRedirect("/page.jsp");
        req.getSession().setAttribute("userinfomsg","个人信息修改成功！");
    }

}
